import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('@/views/Login/index.vue') },
  { path: '/reg', component: () => import('@/views/Reg/index.vue') },
  {
    path: '/',
    component: () => import('@/views/Main/index.vue'),
    children: [
      { path: 'home', component: () => import('@/views/Home/home.vue') },
      { path: 'user-info', component: () => import('@/views/User/userinfo.vue') },
      { path: 'user-avatar', component: () => import('@/views/User/avatar.vue') },
      { path: 'user-pwd', component: () => import('@/views/User/resetpwd.vue') },
      { path: 'art-cate', component: () => import('@/views/Art/artcate.vue') },
      { path: 'art-list', component: () => import('@/views/Art/artlist.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
const white = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  // 如果去的路径不是登录或注册
  // if (to.path!=='/login') {
  if (!white.includes(to.path)) {
    if (store.state.token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
